// Copyright 2011 Google
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

function bind(method, object, arg1, arg2, arg3) {
  return function() { return method.call(object, arg1, arg2, arg3); };
}

function makeId() {
  var rand = (Math.floor(Math.random() * 1000) + 1000 + '').substring(1);
  return (new Date()).getTime() + rand;
}

function listen(target, event, method, object) {
  var args = Array.prototype.slice.call(arguments, 4);
  args.unshift(null);
  function callback(e) {
    args[0] = e;
    return method.apply(object, args);
  }
  if (target && target.nodeType) {
    google.maps.event.addDomListener(target, event, callback);
  } else {
    google.maps.event.addListener(target, event, callback);
  }
}

function create(parent, tag, cls, text, attrs) {
  var element = document.createElement(tag);
  if (cls) {
    element.className = cls;
  }
  if (text) {
    setText(element, text);
  }
  if (attrs) {
    for (var name in attrs) {
      element.setAttribute(name, attrs[name]);
    }
  }
  if (parent) {
    parent.appendChild(element);
  }
  return element;
}

function remove(element) {
  if (element && element.parentNode) {
    element.parentNode.removeChild(element);
  }
}

function clear(element) {
  if (element) {
    element.innerHTML = '';
  }
}

function show() {
  for (var i = 0; i < arguments.length; i++) {
    if (arguments[i]) {
      arguments[i].style.display = '';
    }
  }
}

function hide() {
  for (var i = 0; i < arguments.length; i++) {
    if (arguments[i]) {
      arguments[i].style.display = 'none';
    }
  }
}

function addClass(element, name) {
  if (element) {
    element.className = element.className + ' ' + name;
  }
}

function removeClass(element, name) {
  if (element) {
    var classes = element.className.split(/ +/g);
    var newClasses = [];
    for (var i = 0; i < classes.length; i++) {
      if (classes[i] !== name) {
        newClasses.push(classes[i]);
      }
    }
    element.className = newClasses.join(' ');
  }
}

function setText(element, text) {
  element.textContent = element.innerText = text;
}

function log() {
  if (typeof(console) !== 'undefined') {
    console.log.apply(console, arguments);
  }
}

function describeAge(seconds) {
  seconds = Math.floor(seconds);
  var minutes = Math.floor(seconds/60) % 60;
  var hours = Math.floor(seconds/3600) % 24;
  var days = Math.floor(seconds/3600/24);
  if (seconds < 60) {
    return seconds + ' sec ago';
  } else if (seconds < 3600) {
    return minutes + ' min ago';
  } else if (seconds < 3600 * 24) {
    return hours + ' h ' + minutes + ' min ago';
  } else {
    return days + ' d ' + hours + ' h ' + minutes + ' min ago';
  }
}

if (!('withCredentials' in new XMLHttpRequest())) {
  alert('To use this site, you need Chrome, Safari 4, or Firefox 4 or newer.');
}
